<?php
$con = mysql_connect("localhost","cs143","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("CS143", $con);

if(!empty($_GET['query'])) {
  $q = $_GET['query'];
  $r =  mysql_query($q, $con);
}

?>
<html>
<head><title>CS143 Project 1B</title></head>
<body>
<p>Type an SQL query in the following box:
<p>
<form action="." method="GET">
<textarea name="query" cols="60" rows="8"><?php echo $q; ?></textarea>
<input type="submit" value="Submit" />
</form>
</p>
<p><small>Note: tables and fields are case sensitive. Run "show tables" to see the list of
available tables.</small>
</p>
<?php 
if(!empty($_GET['query'])) {
echo "<h3>Results from MySQL:</h3>

<table border=1 cellspacing=1 cellpadding=2>";


while($row = mysql_fetch_row($r)) {
  echo "<tr>";
  foreach($row as $col) {
  echo '<tr>'.$col.'</tr>';
  }
  echo "</tr>";
}

echo "</table>";
}
mysql_close($con);


?>
</body>
</html>
